<template>
	<view class="padding-bottom-100">
		<form class="flowform" @reset="reset" @submit="submit">
			<view class="formlist inputtext">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>订单编号</label></view>
					<view class="controlvalue">
						<input type="text" v-model="order.orderCode" name="orderCode" disabled="true" />
					</view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="customerName">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>客户名称</label><text style="color: red;">*</text></view>
					<view class="controlvalue">
						<input type="text" v-model="order.customerName" name="customerName" placeholder="选择客户名称" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="orderDate">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>订单日期</label><text style="color: red;">*</text></view>
					<view class="controlvalue">
						<input type="text" v-model="order.orderDate" name="orderDate" placeholder="选择订单日期" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			
			<!-- <view class="formlist inputtext" @tap="orderDates">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>订单日期</label></view>
					<view class="controlvalue">
						<input type="text" v-model="order.orderDates" name="orderDates" placeholder="选择订单日期" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view> -->
			
			
			<view class="formlist inputtext" @tap="salesmanName">
				<view class="formgroup">
					<view class="labletitle"><label>业务人员</label><text style="color: red;">*</text></view>
					<view class="controlvalue">
						<input type="text" v-model="order.salesmanName" name="salesmanName" placeholder="选择业务人员" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			
			<view class="formlist inputtext margin-top-20" @tap="transportMode">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>运输方式</label></view>
					<view class="controlvalue">
						<input type="text" v-model="order.transportMode" name="transportMode" placeholder="选择运输方式" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="paymentMode">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>付款方式</label><text style="color: red;">*</text></view>
					<view class="controlvalue">
						<input type="text" v-model="order.paymentMode" name="paymentMode" placeholder="选择付款方式" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			<view class="formlist inputtext ">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>付款金额</label></view>
					<view class="controlvalue">
						<input type="digit" v-model="order.receivableMoney" name="receivableMoney" placeholder="输入发货金额" :disabled="disabled" />
					</view>
				</view>
			</view>
			<view class="formlist inputtext ">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>定金比率</label></view>
					<view class="controlvalue">
						<input type="digit" v-model="order.earnestRate" name="earnestRate" placeholder="输入定金比率" :disabled="disabled" />
					</view>
				</view>
			</view>
			<view class="formlist inputtext ">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>预付定金</label></view>
					<view class="controlvalue">
						<input type="digit" v-model="order.prepayEarnest" name="prepayEarnest" placeholder="输入发货金额" :disabled="disabled" />
					</view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="deliveryDate">
				<view class="formgroup border-bottom-E3E3E5">
					<view class="labletitle"><label>发货日期</label><text style="color: red;">*</text></view>
					<view class="controlvalue">
						<input type="text" v-model="order.deliveryDate" name="deliveryDate" placeholder="选择发货日期" disabled="true" />
					</view>
					<view class="advanceicon iconfont icon-xiangyou"></view>
				</view>
			</view>
			<view class="formlist">
				<view class="formgroup  padding-17 border-bottom-E3E3E5">
					<view class="labletitletar"><label>发货地址</label></view>
					<view class="controlvalue  uni-textarea">
						<textarea v-model="order.deliveryAddress" name="deliveryAddress" maxlength="500" :disabled="disabled"
						 placeholder="输入" />
						</view>
				</view>
			</view>
			<view class="formlist">
				<view class="formgroup padding-17 border-bottom-E3E3E5">
					<view class="labletitletar"><label>订单备注</label></view>
					<view class="controlvalue  uni-textarea">
						<textarea v-model="order.description" name="description" maxlength="500" :disabled="disabled" placeholder="输入"/>
					</view>
				</view>
			</view>
			
			
			
			<view class="formlist inputtext" style="height: 100%; padding: 20rpx" >
				
			</view>
			
			<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
			
			<!-- #ifndef H5 -->
			<view class="uni-list" v-for="(item, key) in detailproList" :key="key">
			<!-- #endif -->
			
			<!-- #ifdef H5 -->
			<view class="uni-list" v-for="(item, key) in detailproList" :key="'detailpro'+key">
			<!-- #endif -->
				<view class="formgtitle">
					<view class="formgroup">
						<view class="labletitle font-size24"><label>商品添购（{{key+1}}）</label></view>
						<view v-if="detailproList.length>1" class="controlvalue text-align-right font-size24"><text class="color-4869EE" @tap="removeDetailpro(key)">删除</text></view>
					</view>
				</view>
				<view class="formlist inputtext" @tap="goodsName(key)">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>商品名称</label><text style="color: red;">*</text></view>
						<view class="controlvalue">
							<input type="text" v-model="item.goodsName" name="goodsName" placeholder="选择" disabled="true" />
						</view>
						<view class="advanceicon iconfont icon-xiangyou"></view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>规格类型</label></view>
						<view class="controlvalue">
							<input type="text" name="specifications" v-model="item.specifications" placeholder="输入规格类型" disabled="true" />
						</view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>单位</label></view>
						<view class="controlvalue">
							<input type="text" name="unit" v-model="item.unit" placeholder="输入单位" disabled="true"/>
						</view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>数量</label></view>
						<view class="controlvalue">
							<input type="digit" name="qty" v-model="item.qty"  @blur="blurQty(key)" placeholder="输入数量" />
						</view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>单价</label></view>
						<view class="controlvalue">
							<input type="digit" name="price" v-model="item.price" @blur="blurPrice(key)" placeholder="输入单价" />
						</view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>金额</label></view>
						<view class="controlvalue">
							<input type="digit" name="amount" v-model="item.amount" placeholder="输入金额" />
						</view>
					</view>
				</view>
				<view class="formlist">
					<view class="formgroup  padding-17 border-bottom-E3E3E5">
						<view class="labletitletar"><label>备注</label></view>
						<view class="controlvalue  uni-textarea">
							<textarea v-model="item.description" name="description" maxlength="500" :disabled="disabled" placeholder="备注" />
							</view>
					</view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="addDetailpro">
				<view class="formgroup">
					<view class="second-title color-4869EE"><label class="iconfont icon-jia font-size28"></label>商品添购</view>
				</view>
			</view>
			<!-- #ifndef H5 -->
			<view class="uni-list" v-for="(collection, key) in collectionPlanList" :key="key">
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<view class="uni-list" v-for="(collection, key) in collectionPlanList" :key="'collection'+key">
			<!-- #endif -->
				<view class="formgtitle">
					<view class="formgroup">
						<view class="labletitle font-size24"><label>收款计划({{key+1}})</label></view>
						<view v-if="collectionPlanList.length>1" class="controlvalue text-align-right font-size24"><text class="color-4869EE" @tap="removeCollection(key)">删除</text></view>
					</view>
				</view>
				<view class="formlist inputtext" @tap="receivableDate(key)">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>收款日期</label><text style="color: red;">*</text></view>
						<view class="controlvalue">
							<input type="text" v-model="collection.receivableDate" name="receivableDate" placeholder="选择收款日期" disabled="true" />
						</view>
						<view class="advanceicon iconfont icon-xiangyou"></view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>收款比率%</label><text style="color: red;">*</text></view>
						<view class="controlvalue">
							<input type="digit" v-model="collection.receivableRate" name="receivableRate" placeholder="输入收款比率" />
						</view>
					</view>
				</view>
				<view class="formlist inputtext">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>收款金额</label><text style="color: red;">*</text></view>
						<view class="controlvalue">
							<input type="digit" v-model="collection.receivableMoney" name="receivableMoney" placeholder="输入收款金额" />
						</view>
					</view>
				</view>
				<view class="formlist inputtext" @tap="receivableMode(key)">
					<view class="formgroup border-bottom-E3E3E5">
						<view class="labletitle"><label>收款方式</label></view>
						<view class="controlvalue">
							<input type="text" v-model="collection.receivableMode" name="receivableMode" placeholder="选择收款方式" disabled="true" />
						</view>
						<view class="advanceicon iconfont icon-xiangyou"></view>
					</view>
				</view>
				<view class="formlist">
					<view class="formgroup  padding-17 border-bottom-E3E3E5">
						<view class="labletitletar"><label>收款摘要</label></view>
						<view class="controlvalue  uni-textarea">
							<textarea v-model="collection.description" name="description" maxlength="500" :disabled="disabled"
							 placeholder="输入摘要" />
							</view>
					</view>
				</view>
			</view>
			<view class="formlist inputtext" @tap="addCollection">
				<view class="formgroup">
					<view class="second-title color-4869EE"><label class="iconfont icon-jia font-size28"></label>收款计划</view>
				</view>
			</view>
			
			<view class="buttom-box">
				<!-- <view v-if="status== 'null'" class="flow-button">
					<button class="btn-same btn-submit" hover-class="com-hover-btn" form-type="submit" style="width: 100%;">提交</button>
				</view> -->
				
				<!-- <view v-else-if="status== 1" class="flow-button">
					<button class="btn-same" @tap="withdraw" style="background-color: #c41a16; color: #FFFFFF; width: 100%;">撤回</button>
				</view> -->
				
				<!-- <view v-else-if="status==2" class="flow-button">
					<button class="btn-same btn-submit" form-type="submit" hover-class="com-hover-btn">提交</button>
				</view> -->
				
				<!-- <view v-else-if="status== 4" class="flow-button">
					<button class="btn-same btn-keep" @tap="edit" style="background-color: #3281ff; color: #FFFFFF;">确定</button>
				</view> -->
				
				
				<view v-if="status == 'edit'" class="flow-button">
					<button class="btn-same" style="background-color: #f3f3f3;" form-type="reset">重置</button>
					<button class="btn-same btn-keep" @tap="save" style="background-color: #3281ff; color: #FFFFFF;">保存</button>
				</view>
				
				<view v-else-if="status == 'submit'" class="flow-button">
					<button class="btn-same btn-submit" hover-class="com-hover-btn" form-type="submit" style="width: 100%;">提交</button>
				</view>
				
				
				<view v-else class="flow-button">
					<!-- <button class="btn-same" style="background-color: #f3f3f3;" form-type="reset">重置</button> -->
					<button class="btn-same btn-submit" hover-class="com-hover-btn" form-type="submit">提交</button>
					<button class="btn-same btn-keep" @tap="save" style="background-color: #3281ff; color: #FFFFFF;">保存</button>
				</view>
			</view>
		</form>
		<view class="height-100"></view>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
		<tki-tree ref="tree" :range="treelist" :multiple="multiple" :selectParent="selectParent" rangeKey="fullName"></tki-tree>
	</view>
</template>

<script>
	import config from '@/common/js/config.js'
	import helper from '@/common/js/helper.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import tkiTree from '@/components/tki-tree/tki-tree.vue'
	import graceChecker from '@/common/js/graceChecker.js'
	import JNPFUpload from '@/components/JNPFUpload/JNPFUpload.vue'
	let formRule=[
			{name:"customerName",checkType:"notnull",checkRule:"",errorMsg:"选择客户名称"},
			{name:"salesmanName", checkType : "notnull", checkRule:"", errorMsg:"选择业务人员"},
			{name:"paymentMode", checkType : "notnull", checkRule:"", errorMsg:"选择付款方式"},
			{name:"deliveryDate",checkType:"notnull",checkRule:"",errorMsg:"选择发货日期"},
		];
	let collectionRule=[
		{name:"receivableDate",checkType:"notnull",checkRule:"",errorMsg:"选择收款日期"},
		{name:"receivableMoney", checkType : "money", checkRule:"", errorMsg:"输入收款金额"},
		{name:"receivableRate", checkType : "money", checkRule:"",  errorMsg:"输入收款比率"},
		]
	let detailproRule=[
		{name:"goodsName",checkType:"notnull",checkRule:"",errorMsg:"选择商品名称"},
		]
	export default {
		components: {
			tkiTree,
			wPicker,
			JNPFUpload
		},
		data() {
			return {
				title:null,
				status: null,
				order: {
					orderCode:'',
					customerName:'',
					deliveryDate:'',
					salesmanName:'',
					paymentMode:'',
					transportMode:'',
					fileJson:'[]',
					orderDate:''
				},
				collectionPlanList:[],
				detailproList:[],
				disabled:false,
				fileList:[],
				upImgBasic: {
					notli: true,
					isShowAdd:true,
					isEdit:false
				},
				fileJson:[],
				pickerList:[],
				isSearchShow:false,
				mode:'',//底部弹窗分类
				salesmanNameList:[],
				treelist:[],
				customerNameList:[],
				paymentModeList: [{
						label: "现金",
						value: "1"
					},
					{
						label: "转帐",
						value: "2"
					},
					{
						label: "汇票",
						value: "3"
					}
				],
				transportModeList: [{
						label: "快递",
						value: "1"
					},
					{
						label: "物流",
						value: "2"
					},
					{
						label: "配送",
						value: "3"
					},
					{
						label: "自提",
						value: "4"
					}
				],
				multiple:false,
				selectParent:false,
				goodsNameList:[],
				goodsList:[]
			}
		},
		onBackPress(e){
			/* 删除缓存 */
			uni.removeStorageSync("listTitle")
			uni.removeStorageSync("startedTitle")
			uni.removeStorageSync("F_FullName")
		},
		onLoad(e) {
			console.log(e)
			/* 根据状态修改顶部标题 */
			let orderTitle = uni.getStorageSync('listTitle')
			this.title = orderTitle
			if(!e.id){
				this.title = '订单新增'
			}
			/* 设置顶部标题 */
			uni.setNavigationBarTitle({
				title: this.title
			});
			const userProvider = uni.getStorageSync(config.Global.cache + '_UserProvider');
			
			if (Object.prototype.hasOwnProperty.call(e, 'id')) {
				
				/* 保存状态码 */
				this.status = e.status
				console.log(this.status)
				
				if(this.status == 1 || this.status == 'submit'){
					this.disabled = true
				}
				helper.ajax({
					url: '/api/workflow/Form/CrmOrder/' + e.id,
					success: res => {
						let _order = res.data.data;
						this.order = _order
						this.order.orderDate=helper.toDate( _order.orderDate, 'yyyy-MM-dd');
						this.order.deliveryDate=helper.toDate( _order.deliveryDate, 'yyyy-MM-dd');
						this.detailproList= _order.goodsList;
						this.collectionPlanList=_order.collectionPlanList;
						console.log(this.collectionPlanList)
						for (var i = 0; i < _order.collectionPlanList.length; i++) {
							this.collectionPlanList[i].receivableDate=helper.toDate( _order.collectionPlanList[i].receivableDate, 'yyyy-MM-dd');
						}
						if (_order.fileJson!="") {
							this.fileList = JSON.parse(_order.fileJson);
							console.log(this.fileList)
						}
					}
				});
			}else{
				this.status = "add";
				uni.setStorageSync('F_Status',0)
				console.log(this.status)
				helper.ajax({
					url: '/api/system/Base/BillRule/BillNumber/OrderNumber',
					success: res => {
						this.order.orderCode=res.data.data;
					}
				});
				this.collectionPlanList.push({
					receivableDate:'',
					receivableMode:'',
					receivableRate:'',
					receivableMoney:'',
					description:''
				});
				this.detailproList.push({
					goodsName:"",
					specifications:'',
					unit:'',
					qty:'',
					price:'',
					amount:'',
					description:''
				});
			}
			helper.ajax({
				url: 'api/system/Permission/Users/Selector',
				loadingShow:false,
				success: res => {
					this.salesmanNameList = res.data.data.list;
				}
			});
			helper.ajax({
				url: '/api/workflow/Form/CrmOrder/Customer',
				loadingShow:false,
				data:{keyword:''},
				success: res => {
					let customers= res.data.data.list;
					for (var i = 0; i < customers.length; i++) {
						this.customerNameList.push({
							label: customers[i].text,
							value: customers[i].id,
							code: customers[i].code,
							keyword: customers[i].keyword,
						});
					}
				}
			});
			helper.ajax({
				url: '/api/workflow/Form/CrmOrder/Goods',
				loadingShow:false,
				data:{keyword:''},
				success: res => {
					let goods = res.data.data.list;
					for (var i = 0; i < goods.length; i++) {
						this.goodsNameList.push({
							label: goods[i].text,
							value: goods[i].code,
							id: goods[i].id,
							keyword: goods[i].keyword,
							price:goods[i].price,
							unit:goods[i].unit,
							specifications:goods[i].specifications,
						});
					}
				}
			});
		},
		
		methods: {
			salesmanName() {
				this.isSearchShow=false;
				this.treelist = this.salesmanNameList;
				this.selectParent = false;
				this.multiple = false;
				if(!this.disabled){
					this.$refs.tree._show();
				}
				
				this.$eventHub.$off('treemodality');
				this.$eventHub.$on('treemodality', (data) => {
					this.order.salesmanName = data[0].fullName;
					this.order.salesmanId = data[0].id;
					this.$eventHub.$off('treemodality');
				});
			},
			customerName(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.isSearchShow=true;
					this.pickerList = this.customerNameList;
					this.mode="electionsingle";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.customerName = data.result;
						this.$eventHub.$off('popupmodality');
						this.isSearchShow=false;
					});
				}
			},
			paymentMode(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.paymentModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.paymentMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			transportMode(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.transportModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.transportMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			
			orderDate(){
				// console.log('ok')
				if(!this.disabled){
					this.mode="date";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						
						this.order.orderDate = data.result;
						
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			/* 发货日期 */
			deliveryDate(){
				if(!this.disabled){
					this.mode="date";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.deliveryDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			
			receivableDate(index){
				if(!this.disabled){
					this.mode="date";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.collectionPlanList[index].receivableDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			receivableMode(index){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.paymentModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.collectionPlanList[index].receivableMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			/* 选择商品名称 */
			goodsName(index){
				if(!this.disabled){
					this.isSearchShow=true;
					this.pickerList = this.goodsNameList;
					this.mode="electionsingle";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.detailproList[index].goodsName = data.result;
						this.detailproList[index].specifications=this.goodsNameList[data.defaultVal[0]].specifications;
						this.detailproList[index].unit=this.goodsNameList[data.defaultVal[0]].unit;
						this.detailproList[index].qty=1;
						this.detailproList[index].price=this.goodsNameList[data.defaultVal[0]].price;
						this.detailproList[index].amount=(this.goodsNameList[data.defaultVal[0]].price*this.detailproList[index].qty).toFixed(2);
						this.$eventHub.$off('popupmodality');
						this.isSearchShow=false;
					});
				}
			},
			
			blurQty(index){
				this.detailproList[index].amount=(this.detailproList[index].price*this.detailproList[index].qty).toFixed(2);
			},
			blurPrice(index){
				this.detailproList[index].amount=(this.detailproList[index].qty*this.detailproList[index].price).toFixed(2);
			},
			addDetailpro(){
				this.detailproList.push({
					goodsName:"",
					specifications:'',
					unit:'',
					qty:'',
					price:'',
					amount:'',
					description:''
				});
			},
			addCollection(){
				this.collectionPlanList.push({
					receivableDate:'',
					receivableMode:'',
					receivableRate:'',
					receivableMoney:'',
					description:''
				});
			},
			removeDetailpro(index){
				this.detailproList.splice(index,1);
			},
			removeCollection(index){
				this.collectionPlanList.splice(index,1);
			},
			
			setSpinner(list, label) {
				let spinner
				for (var i = 0; i < list.length; i++) {
					if (list[i].label == label) {
						spinner = list[i].value;
						break;
					}
				}
				return spinner
			},
			
			/* 保存 */
			save(e){
				this.order.fileJson = JSON.stringify(this.fileList)
				let formData = this.order
				console.log(formData)
				
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				for(let i=0;i<this.detailproList.length;i++ ){
					// console.log(this.detailproList)
					if (!graceChecker.check(this.detailproList[i], detailproRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				for(let i=0;i<this.collectionPlanList.length;i++ ){
					if (!graceChecker.check(this.collectionPlanList[i], collectionRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				let url = "/api/workflow/Form/CrmOrder";
				let method = "POST";
				if(formData.id){
					url = url+"/"+formData.id;
					method = "PUT";
				}
				formData.customerId = this.setSpinner(this.customerNameList,formData.customerName);
				formData.deliveryDate = Date.parse(formData.deliveryDate);
				formData.orderDate = Date.parse(formData.orderDate);
				for (var i = 0; i < this.collectionPlanList.length; i++) {
					this.collectionPlanList[i].receivableDate=Date.parse(this.collectionPlanList[i].receivableDate);
				}
				formData.goodsList = this.detailproList;
				formData.collectionPlanList = this.collectionPlanList;
				
				formData.status = '1';
				// console.log(formData)
				helper.ajax({
					url: url,
					method: method,
					contentType:"application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg,'success');
						uni.navigateBack();
					}
				});
			},
			
			/* 提交 */
			submit(e){
				this.order.fileJson = JSON.stringify(this.fileList)
				let formData=this.order;
				
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				
				this.isBtnClicks=true;
				this.treelist = this.salesmanNameList;
				this.$refs.tree._show();
				this.$eventHub.$off('treemodality');
				this.$eventHub.$on('treemodality', (data) => {
					this.$eventHub.$off('treemodality');
					formData.customerId = this.setSpinner(this.customerNameList,formData.customerName);
					formData.goodsList = this.detailproList;
					formData.deliveryDate = Date.parse(formData.deliveryDate);
					formData.orderDate = Date.parse(formData.orderDate);
					formData.status = '0';
					formData.freeApproverUserId = data[0].id;
					let method = "POST";
					let url = "/api/workflow/Form/CrmOrder";
					if(formData.id){
						method = "PUT";
						url = url+"/"+formData.id;
					}
					// console.log("formData:",formData)
					for (var i = 0; i < this.collectionPlanList.length; i++) {
						this.collectionPlanList[i].receivableDate=Date.parse(this.collectionPlanList[i].receivableDate);
					}
					formData.collectionPlanList = this.collectionPlanList;
					helper.ajax({
						url: url,
						method:method,
						data:formData,
						contentType: "application/json",
						success: res => {
						helper.msg(res.data.msg, 'success');
						
								// uni.redirectTo({
								// 	url: '../../operate/started/started',
								// 	success: res => {},
								// 	fail: () => {},
								// 	complete: () => {}
								// });
							
								uni.navigateBack();
							
							this.isBtnClicks=false
						}
					});
				});
			},
			
			
			/* 清空数据 */
			reset(e) {
				// console.log('清空数据')
			},
			
			/* 撤回 */
			// withdraw(e){
			// 	// console.log(this)
			// 	if (this.status == 1) { //审核撤回
			// 		this.bus.$off('auditopinion');
			// 		this.bus.$on('auditopinion', (data) => {
			// 			uni.showLoading({
			// 				title: '正在请求...',
			// 				mask: true
			// 			});
			// 			this.isBtnClicks = true;
			// 			let formData=this.order;
			// 			// console.log(data)
			// 			helper.ajax({
			// 				url: `/api/workflow/Form/CrmOrder/${formData.id}/Actions/CancelReview`,
			// 				method: 'POST',
			// 				contentType: "application/json",
			// 				data: {
			// 					handleOpinion: data.F_HandleOpinion,
			// 				},
			// 				success: res => {
			// 					helper.msg(res.data.meg, 'success');
			// 					uni.redirectTo({
			// 						url: '/pages/apply/apply/ordermanagelist/index'
			// 					});
			// 					setTimeout(function() {
			// 						uni.hideLoading();
			// 						this.isBtnClicks = false
			// 					}, 500);
			// 				},
			// 			});
			// 			this.bus.$off('auditopinion');
			// 		});
			// 		uni.navigateTo({
			// 			url:'../../../work/operate/auditopinion/auditopinion?opinion=撤回',
			// 			success: res => {},
			// 			fail: () => {},
			// 			complete: () => {}
			// 		});
			// 	}
			// }
		},
		
	}
</script>

<style>
	page {
		background-color: #f0f2f6;
	}
	.btn-submit{
		width: 50%;
	}
</style>
